# -*- coding: utf-8 -*-
# @Time    : 2020/10/26 16:01
# @Author  : liuwenfan
# @File    : views.py
# @Software: PyCharm
from flask import request

from . import node
from .service import node_service
from ...libs import const
from ...libs.framework import *
from ...libs.validate_params import validate_params


@node.route('/index')
def index():
    return "SUCCESS"


@node.route('/insert_node', methods=["POST"])
@validate_params(params=["node_name", "ipv4", "callback_ip4", "content", "system", "callback_ipv4_port", "ipv4_port"])
def insert_node():
    """
    添加节点
    :return: json
    """
    node_service.add_node(request.json)
    return result(const.SUCCESS, const.INSERT_SUCCESS, None)


@node.route('/delete_node/<id>', methods=["GET"])
def delete_node(id):
    """
    删除节点
    :param id: id
    :return: json
    """
    node_service.del_node(id)
    return result(const.SUCCESS, const.DELETE_SUCCESS, None)


@node.route('/update_node', methods=["POST"])
@validate_params(params=["id", "node_name", "ipv4", "content", "callback_ip4", "system"])
def update_node():
    """
    更新节点
    :return: json
    """
    node_service.update_node(request.json)
    return result(const.SUCCESS, const.EDIT_SUCCESS, None)


@node.route('/find_one/<id>', methods=["GET"])
def find_one(id):
    res = node_service.find_one_node(id)
    return result(const.SUCCESS, None, res)


@node.route('/node_list', methods=["POST"])
@validate_params(params=["page_num", "page_per"])
def node_list():
    """
    获取节点列表
    :return:
    """
    res = node_service.node_list(request.json)
    return result(const.SUCCESS, None, res)


@node.route('/connect_status/<id>', methods=["GET"])
def connect_status(id):
    """
    Telnet
    :param id:
    :return:
    """
    return node_service.obtain_status(id)


@node.route('/get_node', methods=["get"])
def get_node():
    """
    获取节点名称下拉框
    :return:
    """
    res = node_service.get_node()
    return result(const.SUCCESS, None, res)
